package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.event.UserInfoChangeEvent;
import com.cljz.makepassenger.present.activity.IDSettingP;
import com.cljz.makepassenger.utils.date.GetSystemTimeUtil;
import com.cljz.makepassenger.widget.PubPopupWindow;
import com.cljz.makepassenger.widget.datetimpick.PersonTimeSelector;
import com.clzj.makepassenger.bean.UserInfo;
import com.jakewharton.rxbinding.view.RxView;
import com.squareup.picasso.Picasso;
import com.zhy.autolayout.utils.L;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.router.Router;
import de.hdodenhof.circleimageview.CircleImageView;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/25.
 */

public class IDSettingActivity extends BaseActivity<IDSettingP> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.fl_title_right)
    FrameLayout flTitleRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.civ_head_pic)
    CircleImageView civHeadPic;
    @BindView(R.id.ll_head_pic)
    LinearLayout llHeadPic;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.rl_nickname)
    RelativeLayout rlNickname;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.rl_sex)
    RelativeLayout rlSex;
    @BindView(R.id.tv_birthday)
    TextView tvBirthday;
    @BindView(R.id.rl_birthday)
    RelativeLayout rlBirthday;
    @BindView(R.id.tv_bind_phone)
    TextView tvBindPhone;
    @BindView(R.id.rl_bind_phone)
    RelativeLayout rlBindPhone;
    private String selectTime;
    private int sexCode;
    private Map<String, String> userInfoMap;


    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("");
        rlTitle.setBackgroundColor(getResources().getColor(R.color.idSettingBg));
        changeUserInfo();
        initPermission();
        userInfoMap = new HashMap<>();
    }


    private void changeUserInfo() {
        UserInfo userInfo = getP().getLocalUserInfo(getP().getToken());
        Picasso.with(this).load(userInfo.getUser_logo()).placeholder(R.mipmap.logo).into(civHeadPic);
        tvNickname.setText(userInfo.getUser_nickname());
        tvSex.setText("0".equals(userInfo.getUser_sex()) ? "保密" : ("1".equals(userInfo.getUser_sex()) ? "男" : "女"));
        selectTime = userInfo.getBirthday();
        tvBirthday.setText(userInfo.getBirthday());
        tvBindPhone.setText(userInfo.getUser_phone());
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_id_setting;
    }

    @Override
    public IDSettingP newP() {
        return new IDSettingP();
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        exit();
                    }
                });
        RxView.clicks(rlNickname)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance()).to(NicknameEditActivity.class).launch();
                    }
                });
        RxView.clicks(rlSex)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        new PubPopupWindow(IDSettingActivity.this, new PubPopupWindow.SexSelect() {
                            @Override
                            public void select(int position) {
                                sexCode = position;
                                userInfoMap.clear();
                                userInfoMap.put("token", getP().getLocalUserInfo(getP().getToken()).getToken());
                                userInfoMap.put("user_sex", String.valueOf(sexCode));
                                getP().getUserMsgUp(userInfoMap, IDSettingP.UpUserInfo.SEX);
                            }
                        }).showAtLocation(rlTitle, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                    }
                });

        RxView.clicks(rlBirthday)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        new PersonTimeSelector(IDSettingActivity.this, new PersonTimeSelector.ResultHandler() {
                            @Override
                            public void handle(String time) {
                                showLoadingDialog();
                                selectTime = time;
                                userInfoMap.clear();
                                userInfoMap.put("token", getP().getLocalUserInfo(getP().getToken()).getToken());
                                userInfoMap.put("birthday", selectTime);
                                L.e(selectTime);
                                getP().getUserMsgUp(userInfoMap, IDSettingP.UpUserInfo.BIRTHDAY);
                            }
                        }, GetSystemTimeUtil.getStart100Time(), GetSystemTimeUtil.getStartTime(), "保密".equals(tvBirthday.getText().toString().trim()) ? GetSystemTimeUtil.getStartTime() : GetSystemTimeUtil.getTime(GetSystemTimeUtil.getDatetime(selectTime))).show();
                    }
                });

        RxView.clicks(llHeadPic)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance())
                                .putBoolean(UserLogoActivity.EXTRA_SHOW_CAMERA, true)
                                .putInt(UserLogoActivity.EXTRA_SELECT_COUNT, 1)
                                .putInt(UserLogoActivity.EXTRA_SELECT_MODE, UserLogoActivity.MODE_SINGLE)
                                .putIntArray(CutViewActivity.CUT_SCALE, new int[]{1, 1})
                                .to(UserLogoActivity.class)
                                .launch();
                    }
                });
        RxView.clicks(rlBindPhone)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance()).to(PhoneBindingActivity.class).launch();

                    }
                });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        exit();
    }

    private void exit() {
        Router.newIntent(IDSettingActivity.this).to(MainActivity.class).putBoolean("showDrawer",true).launch();
        Router.pop(IDSettingActivity.this);
    }

    public void userMsgUpSucceed(String msg) {
        dismissLoadingDialog();
        BusProvider.getBus().post(new UserInfoChangeEvent());
        showToast(msg, R.mipmap.nearby_success);
    }

    public void userMsgUpFail(String error) {
        showToast(error, R.mipmap.jinshi);

    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    @Override
    public void bindEvent() {
        super.bindEvent();
        BusProvider.getBus().toObservable(UserInfoChangeEvent.class)
                .compose(this.<UserInfoChangeEvent>bindToLifecycle())
                .subscribe(new Action1<UserInfoChangeEvent>() {
                    @Override
                    public void call(UserInfoChangeEvent userInfoChangeEvent) {
                        L.e("UserInfoChangeEvent");
                        changeUserInfo();
                    }
                });
    }
}
